package com.lengxf.leetcode.a;


public class Leetcode_binarySearch {

    static int[] arr = {1, 3, 4, 6, 7, 8, 12, 14, 16};

    /**
     * 二分查找
     **/
    public static void main(String[] args) throws InterruptedException {
        int search = 6;
        int l = 0;
        int r = arr.length;
        int mid;
        while (l <= r) {
            Thread.sleep(500L);
            mid = (l + r - 1) / 2 + 1;
            System.out.println("本次找到了 : " + arr[mid]);
            if (arr[mid] == search) {
                System.out.println("发现了对应元素...");
                return;
            } else if (arr[mid] > search) {
                //中间数值大于要查询的值 则应该查询左部分
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        System.out.println("未找到对应元素");
    }

}
